<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :collapse="sidebarStore.collapse"
      background-color="#001529"
      text-color="#fff"
      active-text-color="#89d961"
      router
      :default-active="onRoutes"
    >
      <template v-for="item in menu" :index="item.id">
        <template v-if="item.subs">
          <el-sub-menu :index="item.title" :key="item.id">
            <template #title>
              <base-icon :base-icon="item.icon" />
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-sub-menu v-if="subItem.subs" :index="subItem.url" :key="subItem.id">
                <template #title>
                  <base-icon :base-icon="subItem.icon" />
                  <span>{{ subItem.title }}</span>
                </template>
                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.url">
                  <base-icon :base-icon="threeItem.icon" />
                  <span>{{ threeItem.title }}</span>
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item :index="subItem.url">
                <base-icon :base-icon="subItem.icon" />
                <span>{{ subItem.title }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item :index="item.url" :key="item.id">
            <base-icon :base-icon="item.icon" />
            <span>{{ item.title }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script setup lang="ts">
  import { computed } from 'vue'
  import { useRoute } from 'vue-router'
  import { sessionCache } from '@/utils/cache'
  import useSidebarStore from '@/store/sidebar'
  const route = useRoute()
  const onRoutes = computed(() => {
    return route.path
  })
  const { menu } = sessionCache.getCache('userInfo')

  const defaultOpen: any = []
  menu.forEach((item: any) => {
    defaultOpen.push(item.title)
  })

  const sidebarStore = useSidebarStore()
</script>

<style scoped>
  .sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    overflow-y: scroll;
  }

  .sidebar::-webkit-scrollbar {
    width: 0;
  }

  .sidebar-el-menu:not(.el-menu--collapse) {
    width: 230px;
  }

  .sidebar > ul {
    height: 100%;
  }
</style>
